package cn.changshengjue.dakong.command;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import org.bukkit.entity.Player;
import org.bukkit.util.StringUtil;

import cn.changshengjue.dakong.main;
import cn.changshengjue.dakong.command.type.AdminCommand;
import cn.changshengjue.dakong.config.cfg;

public class CommandsExecutor implements CommandExecutor, TabExecutor {
	// 一些判断 没子指令就出示帮助指令
	public boolean onCommand(CommandSender commandSender, Command command, String label, String[] args) {
		if (args.length == 0) {
			sendHelp(commandSender);
			return false;
		} else {
			String arg = args[0];
			SubCommand subCommand = main.getCommandHandler().getSubCommand(arg);
			if (subCommand == null) {
				subCommand = main.getCommandHandler().getSubCommand("*");
			}
			if (subCommand == null) {
				return false;
			}
			// 判断有没有Admin权限
			if (subCommand instanceof AdminCommand) {
				if (commandSender.isOp()) {
					if (subCommand.isNeedArg()) {
						int argLength = subCommand.getArgLength();
						if (argLength != args.length) {
							subCommand.sendHelp(commandSender);
							return false;
						} else {
							return subCommand.onCommand(commandSender, command, label, args);
						}
					} else {
						return subCommand.onCommand(commandSender, command, label, args);
					}
				} else {
					commandSender.sendMessage(cfg.MSG_PLUGIN + "§c权限不足");
					return false;
				}
			} else {
				if (subCommand.isNeedArg()) {
					int argLength = subCommand.getArgLength();
					if (argLength != args.length) {
						subCommand.sendHelp(commandSender);
						return false;
					} else {
						return subCommand.onCommand(commandSender, command, label, args);
					}
				} else {
					return subCommand.onCommand(commandSender, command, label, args);
				}
			}
		}
	}

	// 帮助指令格式
	private void sendHelp(CommandSender sender) {
		main.getCommandHandler().getCommands().stream().filter(subCommand -> !(subCommand instanceof AdminCommand))
				.forEach(subCommand -> sender
						.sendMessage("§7" + subCommand.getUsage() + " " + subCommand.getDescription()));
		if (sender.isOp()) {
			main.getCommandHandler().getCommands().stream().filter(subCommand -> subCommand instanceof AdminCommand)
					.forEach(subCommand -> sender
							.sendMessage("§7" + subCommand.getUsage() + " " + subCommand.getDescription()));
		}
	}

	@Override
	public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
		final List<String> completions = new ArrayList<String>();

		List<String> commands = new ArrayList<String>();
		if (sender instanceof Player) {
			if (sender.isOp()) {
				commands.add("give");
				commands.add("set");
				commands.add("item");
				commands.add("reload");
				commands.add("start");
			} else {
				commands.add("start");
			}
		}
		if (commands.size() == 0) {
			return null;
		}
		StringUtil.copyPartialMatches(args[args.length - 1].toLowerCase(), (Iterable) commands,
				(Collection) completions);
		Collections.sort(completions);
		return completions;
	}

}
